package org.mozhu.learning.algo.stack;

import java.util.EmptyStackException;
import java.util.LinkedList;

/**
 * 基于JDK LinkedList 实现的Stack
 */
public class StackOfJDKLinkedList<E> implements Stack<E> {

    private LinkedList<E> datas = new LinkedList<>();

    @Override
    public int size() {
        return datas.size();
    }

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

    @Override
    public void push(E element) {
        datas.addFirst(element);
    }

    @Override
    public E pop() {
        if(isEmpty()){
            throw new EmptyStackException();
        }
        return datas.removeFirst();
    }

    @Override
    public E peak() {
        if(isEmpty()){
            throw new EmptyStackException();
        }
        return datas.getFirst();
    }
}
